setwd("/home1/30/jc227089/SRE/gvar_adap/PBS")

stps<-c(1:5)
lda<-c(2, 6, 10)
dct<-c(0, 0.2)
dvl<-c(seq(0.1,1,0.1), 99)

script.file<-'/home1/30/jc227089/evo-dispersal/SRE/gvar_adap/Gradient_var_adaptation.R'

for (ii in 1:length(stps)) {
	for (jj in 1:length(lda)){
		for (kk in 1:length(dct)){
			for (ll in 1: length(dvl)){
				fid<-(ii-1)*length(lda)*length(dct)*length(dvl)+(jj-1)*length(dct)*length(dvl)+(kk-1)*length(dvl)+ll #file.ID
				##create the sh file
				zz = file(paste("SREgvar", fid,'.sh',sep=''),'w')
				cat('##################################\n',file=zz)
				cat('#!/bin/sh\n',file=zz)
				cat('cd $PBS_O_WORKDIR\n',file=zz)
				cat("R CMD BATCH --no-save --no-restore '--args rr=3 file.ID=",fid, " stps=", stps[ii], " lv=", lda[jj], " dc=", dct[kk], " dvl=", dvl[ll], "' ", sep="", file=zz)
				cat(script.file, " ", paste("SREgvar", fid,'.Rout',sep=''), "\n", sep="",file=zz)
				cat('##################################\n',file=zz)
				close(zz)
			
				#submit the job
				system(paste("qsub -m n ", paste("SREgvar", fid,".sh",sep=""),sep=""))
			}
		}
	}
}
